In this notebook, more advanced transformations using PyTorch's torchvision.transforms.
The following transformations are applied to an example image.
The Image used is this one with JPEG format,size :(4288, 2848) and RGB model.
from PIL import Image
import torch
import torchvision
import matplotlib.pyplot as plt
"""Load Image"""
# Open the image file
img = Image.open('image.jpg')
# View img properties
print('Image Size',img.size)
print('Image Mode',img.mode)
print('Image Format',img.format)
# Show the image
img.show()
Image Size (640, 427) Image Mode RGB Image Format JPEG
"""Image to Tensor """
# Set tranformation
transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor(),
])
# Apply transformation to image
tensor_image = transform(img)
# Print tensor shape
print(tensor_image.shape) # (Channels, Height ,Width)
torch.Size([3, 427, 640])
"""Normalize Image"""
# Numbers represent Channels
# torchvision.transforms.Normalize([mean_1, mean_2, mean_3], [std_1, std_2, std_3])
#Example:
torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
# ApplyTransformation
normalized = transform(img)
# View Img
# Transpose tensor to (height, width, channels) order
tensor_image = normalized.permute(1, 2, 0)
# Convert tensor to numpy array
numpy_image = tensor_image.numpy()
# Display image using matplotlib
plt.imshow(numpy_image)
<matplotlib.image.AxesImage at 0x7ff882113f70>
""" Center Crop """
# Tranformation
transform = torchvision.transforms.CenterCrop((400,400))
# Apply tranform
cropped= transform(img)
# View Img
cropped.size
plt.imshow(cropped)
<matplotlib.image.AxesImage at 0x7ff87efde7f0>
"""Random Horizontal Flip"""
transform = torchvision.transforms.RandomHorizontalFlip(p=0.9)
transformed= transform(img)
plt.imshow(transformed)
<matplotlib.image.AxesImage at 0x7ff87ef94b80>
"""RandomRotation"""
transform = torchvision.transforms.RandomRotation(degrees=30)
transformed = transform(img)
plt.imshow(transformed)
<matplotlib.image.AxesImage at 0x7ff87edc4700>
"""Color : Grayscale"""
transform = torchvision.transforms.Grayscale(num_output_channels=1)
grayscale = transform(img)
plt.imshow(grayscale)
<matplotlib.image.AxesImage at 0x7ff87ee9f670>
""" Gaussian Blurr """
transform = torchvision.transforms.GaussianBlur(kernel_size=501)
blurred = transform(img)
plt.imshow(blurred)
<matplotlib.image.AxesImage at 0x7ff87eb849a0>
""" Brightness"""
bright = torchvision.transforms.functional.adjust_brightness(img,brightness_factor=3.8)
plt.imshow(bright)
<matplotlib.image.AxesImage at 0x7ff87ea59d60>
""" Contrast """
contrasted = torchvision.transforms.functional.adjust_contrast(img,contrast_factor=5.8)
plt.imshow(contrasted)
<matplotlib.image.AxesImage at 0x7ff87e6f51f0>
""" Hue """
hued = torchvision.transforms.functional.adjust_hue(img,hue_factor=0.5)
plt.imshow(hued)
<matplotlib.image.AxesImage at 0x7ff87e565430>
""" Sharpness """
sharp = torchvision.transforms.functional.adjust_sharpness(img,sharpness_factor=8)
plt.imshow(sharp)
<matplotlib.image.AxesImage at 0x7ff87e4ea850>
""" Histogram Equalizing """
equal = torchvision.transforms.functional.equalize(img)
plt.imshow(equal)
<matplotlib.image.AxesImage at 0x7ff87e959cd0>